package simtester.agent;

import static org.junit.Assert.*;

import groovy.mock.interceptor.MockFor
import groovy.mock.interceptor.StubFor
import groovyx.net.http.ContentType
import groovyx.net.http.RESTClient
import java.util.logging.Logger

import org.junit.Test

class RESTCliTests {
    def tcId = '222222222222222222222222'

    @Test
    void logMsgFormat() {
        def mockLogger = new MockFor(Logger)
        
        def logmsg
        mockLogger.ignore('getLogger')
        mockLogger.ignore('addHandler')
        mockLogger.ignore('getHandlers')
        mockLogger.demand.with {
            info { msg ->
                logmsg = msg
            }
        }

        mockLogger.use {
            RESTCli server = new RESTCli('http://www.google.com/', ContentType.HTML, testcaseId: tcId, user: 3, iter: 2)
            server.get(path: '')
        }
        
        assert logmsg =~ /^tc:$tcId user:3 iter:2 resp:\d+ type:GET\|http:\/\/www\.google\.com\/ content-type:text\/html/
    }
    
    @Test
    void logMsgForHttpRequestExceptions() {
        def mockLogger = new MockFor(Logger)
        
        def logmsg
        mockLogger.ignore('getLogger')
        mockLogger.ignore('addHandler')
        mockLogger.ignore('getHandlers')
        mockLogger.demand.with {
            info { msg ->
                logmsg = msg
            }
        }

        mockLogger.use {
            // Use bogus address to force an exception
            RESTCli server = new RESTCli('http://www.lksanvzcdfhourew.com/', ContentType.HTML, testcaseId: tcId, user: 3, iter: 2)
            
            try {
                server.get(path: '')
            } catch (Exception e) {}
        }
        
        assert logmsg =~ /^tc:$tcId user:3 iter:2 type:GET\|http:\/\/www\.lksanvzcdfhourew\.com\/ content-type:text\/html/
    }
}
